﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.Reflection;
using ATM_Pro.Module;
using System.IO;
using System.Diagnostics;
using ATM_Pro.Public;

namespace ATM_Pro.Query
{
    public partial class WarnInfoQeury : Form
    {
        public WarnInfoQeury()
        {
            InitializeComponent();

        }
        private List<ATM> Atms { get; set; }

        //init control cmbAtmAddress
        private void InitCombox()
        {
            cmbCamera.Items.Clear();
            cmbAtmAddress.Items.Clear();
            cmbWarnType.Items.Clear();
            Atms = Configuration.GetInstance().Atms;
            foreach (ATM atm in Atms)
            {
                cmbAtmAddress.Items.Add(Enum.GetName(typeof(AtmAddress), atm.AddId));
            }

            MemberInfo[] mebInfo = typeof(WarnType).GetMembers();
            foreach (MemberInfo mi in mebInfo)
            {
                if (mi.MemberType == MemberTypes.Field)
                {
                    string types = mi.Name.ToString();
                    if (!types.Equals("value__"))
                    {
                        cmbWarnType.Items.Add(types);
                    }
                }
            }
        }

        private void WarnInfoQeury_Load(object sender, EventArgs e)
        {
            InitCombox();
            tsbBeginSearch.Enabled = false;
        }

        #region

        private void cmbAtmAddress_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbCamera.Items.Clear();
            int index = (int)Enum.Parse(typeof(AtmAddress), cmbAtmAddress.Text);
            foreach (ATM atm in Atms)
            {
                if (atm.AddId == index)
                {
                    foreach (Camera cam in atm.Cams)
                    {
                        cmbCamera.Items.Add(cam.Name);
                    }
                }
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0, 30);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        #endregion

        private void tspFirst_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(tspHide.Text.Trim()) == 0)
                return;
            BindData(0, 30);
        }

        private void BindData(int index, int pagecount)
        {

            QeuryInfoHandle query = new QeuryInfoHandle();
            DateTime bdate = dtpBeginTime.Value;
            DateTime btime = tedBeginTime.Time;

            DateTime edate = dtpEndTime.Value;
            DateTime etime = tedEndTime.Time;

            DateTime begintime = new DateTime(bdate.Year, bdate.Month, bdate.Day, btime.Hour, btime.Minute, btime.Second);
            DateTime endtime = new DateTime(edate.Year, edate.Month, edate.Day, etime.Hour, etime.Minute, etime.Second);

            string atmName = cmbAtmAddress.Text;
            string camName = cmbCamera.Text;
            string type = cmbWarnType.Text;
            string msg = "";

            if (QeuryInfoAnalyze.CheckQueryParams(begintime, endtime, atmName, camName, type, true, ref msg))
            {
                int count = 0;
                DataSet resWarnInfo = query.Query(begintime, endtime, atmName, camName, type, index, pagecount, ref count);

                if (resWarnInfo != null && resWarnInfo.Tables.Count > 0 && resWarnInfo.Tables[0].Rows.Count > 0)
                {
                    ShowDataInToolBar(count, pagecount, index);
                    dgvResult.DataSource = resWarnInfo.Tables[0];
                }
                else
                {
                    MessageBox.Show("没有找到相关信息！");
                    dgvResult.DataSource = resWarnInfo;
                }
            }
            else
            {
                MessageBox.Show(msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        int countpage = 0;

        private void ShowDataInToolBar(int count, int pagecount, int index)
        {
            countpage = (count % pagecount > 0) ? (count / pagecount) + 1 : (count / pagecount);
            tspHide.Text = index.ToString();
            if (countpage >= 2) tsbBeginSearch.Enabled = true;
            tspAll.Text = string.Format("第 {0}/{1} 页", index + 1, countpage);
        }

        private void tspPre_Click(object sender, EventArgs e)
        {
            int dd = Convert.ToInt32(tspHide.Text.Trim()) - 1;
            if (dd < 0) return;
            BindData(dd, 30);
        }

        private void tspNext_Click(object sender, EventArgs e)
        {
            int dd = Convert.ToInt32(tspHide.Text.Trim()) + 1;
            if (dd >= countpage) return;
            BindData(dd, 30);
        }


        private void tspEnd_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(tspHide.Text.Trim()) == countpage - 1) return;
            BindData(countpage - 1, Convert.ToInt32(tspHide.Text.Trim()) + 1);
        }

        private void tspBeginSearch_Click(object sender, EventArgs e)
        {
            try
            {
                int val = Convert.ToInt32(txtJump.Text.ToString());
                if (!(val > 0 && val <= countpage))
                {
                    MessageBox.Show("输入跳转页数为1-{0}的整数！", countpage.ToString());
                    return;
                }
                BindData(val - 1, 30);
            }
            catch (FormatException ex)
            {
                MessageBox.Show("请输入正整数！");
                return;
            }

        }

        private void dgvResult_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvResult.Focused)
            {

                DataGridViewRow dr = dgvResult.CurrentRow;
                if (dr == null) return;
                int curIndex = dr.Cells[0].RowIndex;

                string path = "";
                if (dr.Cells["wImgPath"].Value !=null)
                {
                    path = dr.Cells["wImgPath"].Value.ToString();
                }

                string transPath = "";
                if (dr.Cells["cTransImgPath"].Value != null)
                { 
                    transPath = dr.Cells["cTransImgPath"].Value.ToString();
                }

                string temp = ImageHandle.AnalyzLiveImgFileName(path);
                if (picWarnInfo.Image != null)
                {
                    picWarnInfo.Image.Dispose();
                    picWarnInfo.Image = null;
                }

                if (temp != "")
                {
                    if (File.Exists(temp))
                    {
                        Image img = Image.FromFile(temp);
                        picWarnInfo.Image = img;
                    }
                }

                //temp = ImageHandle.AnalyzTransImgFileName(path);
                //if (temp != "")
                //{
                //    Image img = Image.FromFile(temp);
                //    if (picTransInfo.Image != null)
                //    {
                //        picTransInfo.Image.Dispose();
                //        picTransInfo.Image = null;
                //    }
                //    picTransInfo.Image = img;
                //}

                if (picTransInfo.Image != null)
                {
                    picTransInfo.Image.Dispose();
                    picTransInfo.Image = null;
                }
                if (transPath !="")
                {
                    Image img = Image.FromFile(transPath);
                    picTransInfo.Image = img;
                }

                QeuryInfoHandle queryInfo = new QeuryInfoHandle();
                DataTable dt = queryInfo.GetTransInfoByPath(transPath);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    lblTransCardID.Text = string.Format("交易卡号 {0}", row["tCardId"].ToString());
                    lblTransTime.Text = string.Format("交易时间 {0}", row["tTransTime"].ToString());
                    lblTransAddress.Text = string.Format("交易地点 {0}", row["Atm_Address"].ToString());
                    lblCameraId.Text = string.Format("照相机位置 {0}", row["Cam_Name"].ToString());
                }
            }
        }

        private void dgvResult_DoubleClick(object sender, EventArgs e)
        {
            if (dgvResult.Focused == true && dgvResult.CurrentRow != null)
            {
                DataGridViewRow dr = dgvResult.CurrentRow;
                int curIndex = dr.Index;
                if (curIndex > 0)
                {
                    VideoHandle vhandle = new VideoHandle();
                    string path = dr.Cells["wImgPath"].Value.ToString();

                    string fileName = GeneratorVideoPath(path);
                    if (vhandle.HasVideo(fileName))
                        Process.Start(fileName);
                    else
                        MessageBox.Show("没有找到相关视频！");
                }
            }
        }

        private string GeneratorVideoPath(string path)
        {
            string fileName = Path.GetFileName(path);
            path = path.Replace(@"\" + fileName, ".avi");
            return path.Replace(PacketType.AlarmMessage.ToString(), PacketType.RealtimeMessage.ToString());
        }


    }
}